package org.come.control;

import java.math.BigDecimal;

import org.come.Frame.Change_titleJframe;
import org.come.Jpanel.Change_titleJpanel;
import org.come.action.FromServerAction;
import org.come.bean.LoginResult;
import org.come.bean.RoleShow;
import org.come.bean.TitleBean;
import org.come.socket.GameClient;
import org.come.until.FormsManagement;
import org.come.until.GsonUtil;

import com.tool.ModerateTask.TaskRoleData;
import com.tool.image.ImageMixDeal;
import com.tool.role.RoleData;

public class RoleChangeAppellationControl implements FromServerAction {

	@Override
	public void controlMessFromServer(String mes, String type) {

		// 依据协议头进行划分方法
		// String mesProcess[] = mes.split("//");
		switch (type) {
		case "titlelist":
			try {
				titleList(mes);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case "titlechange":
			titleChange(mes);
			break;

		}

	}

	/**
	 * 显示角色称谓列表
	 * 
	 * @throws Exception
	 */
	public void titleList(String mes) throws Exception {
		TitleBean titleBean = GsonUtil.getGsonUtil().getgson().fromJson(mes, TitleBean.class);
		LoginResult loginResult = RoleData.getRoleData().getLoginResult();
		Change_titleJpanel titleJpanel = Change_titleJframe.getChange_titlejframe().getChange_titlejpanel();
		// 将当前称谓放进当前称谓框
		titleJpanel.getLabnamech().setText(loginResult.getTitle());
		// 则先清空列表
		if (titleJpanel.getModelname() != null) {
			titleJpanel.getModelname().clear();
		}
		// 当称谓列表不为空时
		if (titleBean.getTitletables() != null) {
			for (int i = 0; i < titleBean.getTitletables().size(); i++) {// 将玩家的称谓列表展示出来
				titleJpanel.getModelname().addElement(titleBean.getTitletables().get(i).getTitlename());
			}
		}
		// 判断是否有加入帮派
		if (loginResult.getGang_id() != null && loginResult.getGangname() != null && loginResult.getGangpost() != null) {
			titleJpanel.getModelname().addElement(loginResult.getGangname() + loginResult.getGangpost());
		}
		// 判断是否有结婚
		if (loginResult.getMarryObject() != null && !"".equals(loginResult.getMarryObject())) {
			if ("男".equals(loginResult.getSex())) {
				titleJpanel.getModelname().addElement(loginResult.getMarryObject() + "的老公");
			} else if ("女".equals(loginResult.getSex())) {
				titleJpanel.getModelname().addElement(loginResult.getMarryObject() + "的老婆");
			}
		}
		double max = loginResult.getKilltype("击杀煞星");
		if (max >= 7) {
			titleJpanel.getModelname().addElement("地煞斗士");
		} else if (max >= 11) {
			titleJpanel.getModelname().addElement("地煞克星");
		} else if (max >= 15) {
			titleJpanel.getModelname().addElement("地煞战神");
		}
		int jqId = TaskRoleData.SumReceive(110, 3);
		/**
		 * 1称 3163 2称 3171 3称 3178 4称 3193 5称 3201 6称 3211 7称 3221 8称 3232
		 * 9称3242 38行
		 */
		int nowTitle = getNowTitle(jqId);
		BigDecimal race_id = loginResult.getRace_id();
		String[] NumTitle;
		if (race_id.compareTo(new BigDecimal(10001)) == 0) {
			NumTitle = new String[] { "武林新丁", "江湖小虾", "后起之秀", "武林高手", "风尘奇侠", "无双隐士", "世外高人", "江湖侠隐", "无敌圣者", "三界贤君" };
		} else if (race_id.compareTo(new BigDecimal(10002)) == 0) {
			NumTitle = new String[] { "古灵精怪", "魅力精灵", "魔幻使者", "妖之奇葩", "天神煞星", "万兽妖灵", "混世魔王", "三界妖仙", "魔神至尊", "齐天妖王" };
		} else if (race_id.compareTo(new BigDecimal(10003)) == 0) {
			NumTitle = new String[] { "仙灵小童", "逍遥之仙", "陆地飞仙", "无极天师", "神机真人", "降魔金仙", "金身尊者", "太外飞仙", "万圣天尊", "九天圣佛" };
		} else {
			NumTitle = new String[] { "阴曹小鬼", "飘渺之魂", "幽冥鬼士", "勾魂使者", "催命判官", "阴阳无常", "阎罗鬼王", "冥灵鬼仙", "九幽神君", "阴都大帝" };
		}
		for (int i = 0; i < nowTitle; i++) {
			titleJpanel.getModelname().addElement(NumTitle[i]);
		}
		// 加上新手称谓
		titleJpanel.getModelname().addElement(GameClient.BT+"一员");// GameClient.potAndIpStrings[3]
		FormsManagement.showForm(10);
	}

	// 判断当前可以获取几称称谓
	public int getNowTitle(int jqId) {
		if (jqId >= 3265) {
			return 9;
		} else if (jqId >= 3253) {
			return 8;
		} else if (jqId >= 3239) {
			return 7;
		} else if (jqId >= 3227) {
			return 6;
		} else if (jqId >= 3215) {
			return 5;
		} else if (jqId >= 3204) {
			return 4;
		} else if (jqId >= 3187) {
			return 3;
		} else if (jqId >= 3178) {
			return 2;
		} else if (jqId >= 3166) {
			return 1;
		}
		return 0;
	}

	/** 更改称谓 */
	public void titleChange(String mes) {
		RoleShow roleShow = GsonUtil.getGsonUtil().getgson().fromJson(mes, RoleShow.class);
		// 判断是自己还是别人
		// 是自己
		if (ImageMixDeal.userimg.getRoleShow().getRole_id().compareTo(roleShow.getRole_id()) == 0) {
			ImageMixDeal.userimg.getRoleShow().setTitle(roleShow.getTitle());
			ImageMixDeal.userimg.setAppellation(roleShow.getTitle());
		} else {// 是别人
			ImageMixDeal.Playerimgmap.get(roleShow.getRolename()).getRoleShow().setTitle(roleShow.getTitle());
			ImageMixDeal.Playerimgmap.get(roleShow.getRolename()).setAppellation(roleShow.getTitle());
		}
		FormsManagement.HideForm(10);
	}
}
